<?
ob_start();
session_start();
$keyword		= $HTTP_GET_VARS['keyword'];
$show			= $HTTP_GET_VARS['show'];
$st				= $HTTP_GET_VARS['st'];
$b2				= $HTTP_GET_VARS['b2'];
$b1				= $HTTP_GET_VARS['b1'];
$t2				= $HTTP_GET_VARS['t2'];
$t1				= $HTTP_GET_VARS['t1'];
$filter			= $HTTP_GET_VARS['filter'];

include("config.php");

// cek hak akses untuk cabang
$sad = mssql_query("SELECT nama_menu FROM VPrivUser A, VCabang B WHERE username='".$HTTP_SESSION_VARS['user_login']."' AND baca='1' AND A.nama_menu=B.Kode_Cabang",$conn);
while($rad = mssql_fetch_array($sad)){
// VARIABLE UNTUK CABANG
$VAreaCode[] = $rad[0];
}

$sql  = "select A.NIK, A.Nama, A.no_surat, TglBerlaku, Tglkeluar, category, sanksi, keterangan, kode_so2, jabatan2, Jenis from vsanksi A, vlatestkarir B, MstSO C where A.NIK=B.NIK AND B.kode_so2=C.kode_so AND C.AreaCode IN (";

for($k=0;$k<count($VAreaCode);$k++){
	$sql .= "'$VAreaCode[$k]',";
}

$sql 	.= "'$VAreaCode[0]')";	

if(!empty($keyword)){
	$sql .= " AND A.NIK LIKE '%$keyword%' OR A.Nama LIKE '%$keyword%'";
}

if(!empty($filter)){
	$sql .=" AND A.category='".$filter."'";
}

if(!empty($st)){
	if($st == "bekerja"){
		$sql .=" AND Jenis NOT IN ('U','H')";
	}
	else{
		$sql .=" AND Jenis IN ('U','H')";
	}
}

if(!empty($b1) && !empty($b2)){
	$sql .= " AND (month(tgl_laku) BETWEEN '".$b1."' AND '".$b2."') ";
} 
	
if(!empty($t1) && !empty($t2)){
	$sql .= " AND (year(tgl_laku) BETWEEN '".$t1."' AND '".$t2."')";
}

$sql .= " order by A.NIK";

$query = mssql_query($sql,$conn);

echo"<table>";
//A.NIK, A.Nama, A.no_surat, TglBerlaku, Tglkeluar, sanksi, keterangan, kode_so, jabatan, Jenis
echo"<tr>
	<td>NIK</td>
	<td>Nama</td>
	<td>No Surat</td>
	<td>Tgl Berlaku</td>
	<td>Tgl Keluar</td>
	<td>Kategori</td>
	<td>Sanksi</td>
	<td>Keterangan</td>
	<td>Unit Kerja</td>
	<td>Jabatan</td>
	<td>Status Kerja</td>
</tr>";
while($row = mssql_fetch_array($query)){
	echo"
	<tr>
	 <td>$row[NIK]</td>
	 <td>$row[Nama]</td>
	 <td>$row[no_surat]</td>
	 <td>$row[TglBerlaku]</td>
	 <td>$row[Tglkeluar]</td>
	 <td>";
	 if(!empty($row[category])){
		 $sq = mssql_query("SELECT kode,nama FROM VKatSanksi WHERE kode='".$filter."'",$conn);
		 while($rq = mssql_fetch_array($sq)){
			echo"$rq[nama]";
		 }
	 }
	echo"</td>
	 <td>$row[sanksi]</td>
	 <td>$row[keterangan]</td>
	 <td>";
	 if(!empty($row[kode_so2])){
	 	$sun = mssql_query("SELECT uraian FROM MstSO WHERE kode_so='".$row[kode_so2]."'");
		$run = mssql_fetch_array($sun);
		echo"$run[0]";
	 }
	 echo"</td><td>";
	 if(!empty($row[jabatan2])){
	 	$sun2 = mssql_query("SELECT jabatan FROM MstJabatan WHERE kode='".$row[jabatan2]."'");
		$run2 = mssql_fetch_array($sun2);
		echo"$run2[0]";
	 }
	 echo"</td><td>";
	if($row[Jenis] == "U" || $row[Jenis] == "H"){
		echo"Berhenti";
	}
	else{
		echo"Bekerja";
	}
    echo"</td>
	</tr>
	";
}
echo"</table>";
// export to excel

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=DownloadSanksiKaryawan.xls");
header("Pragma: no-cache");
header("Expires: 0");
//*/
?>